package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseBaseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService  courseBaseService;

    @GetMapping("/course/all")
    public List<CourseBase> queryAll() {
        List<CourseBase> courseBases = courseBaseService.queryAll();
        return courseBases;
    }

    @PostMapping("/course/list")
    public PageVO queryCourseBaseList(PageRequestParams params,
                                      @RequestBody QueryCourseBaseModel model) {
        Long companyId = SecurityUtil.getCompanyId();
        PageVO pageVO = courseBaseService.queryCourseBaseList(params, model,companyId);
        return pageVO;
    }

    @PostMapping("/course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);

        Long companyId = SecurityUtil.getCompanyId();
        dto.setCompanyId(companyId);

        CourseBaseDTO courseBase = courseBaseService.createCourseBase(dto);
        return courseBase;
    }

    @GetMapping("/course/{courseBaseId}")
    public CourseBaseDTO getCourseBaseById(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        CourseBaseDTO dto = courseBaseService.getCourseBaseById(courseBaseId, companyId);
        return dto;
    }

    @PutMapping("/course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        Long companyId = SecurityUtil.getCompanyId();
        CourseBaseDTO toDto = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        toDto.setCompanyId(companyId);
        CourseBaseDTO dto = courseBaseService.modifyCourseBase(toDto);
        return dto;
    }

    @DeleteMapping("/course/{courseBaseId}")
    public Boolean deleteCourseBaseById(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        Boolean bool = courseBaseService.deleteCourseBaseById(courseBaseId,companyId);
        return bool;
    }
}
